class Queue {
  constructor(n) {
    this.arr = new Array(n);
    this.head = 0;
    this.tail = 0;
  }
}

// 入队
Queue.prototype.enqueue = function(val){
  if(this.full()) return;
  this.arr[this.tail] = val;
  this.tail += 1;
}

// 出队
Queue.prototype.dequeue = function(){
  if(this.empty()) return;
  const item = this.arr[this.head];
  this.head += 1;
  return item;
}

// 查看队首元素
Queue.prototype.front = function(){
  if(this.empty()) return;
  return this.arr[this.head];
}

// 尾指针指向最后一个元素的下一位 就说明满了
Queue.prototype.full = function(){
  return this.tail === this.arr.length;
}

Queue.prototype.empty = function(){
  return this.head === this.tail;
}

Queue.prototype.size = function(){
  return this.tail - this.head;
}

Queue.prototype.output = function(){
  return this.arr.slice(this.head, this.tail)
}

function test(opts, vals) {
  let queue = new Queue(5);

  for (let i = 0; i < opts.length; i++) {
    switch(opts[i]) {
      case 'insert':
        queue.enqueue(vals[i]);
        break;
      case 'front':
        console.log(`front element: ${queue.front()}`);
        break;
      case 'dequeue':
        queue.dequeue();
        break;
      case 'size':
        console.log(`queue size: ${queue.size()}`);
        break;
      default:
        break;
    }
    console.log(queue.output());
  }
}

let opts = ['insert', 'insert', 'insert','front', 'insert', 'insert', 'dequeue', 'size'];
let vals = [1, 2, 3, '', 4, 5, '', '']
test(opts, vals)
